package com.ape.controller;

import com.ape.redis.utils.CacheUtil;
import com.ape.redis.utils.RedisShareLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("test")
public class TestController {

    @Autowired
    private RedisShareLockUtil redisShareLockUtil;

    @Autowired
    private CacheUtil cacheUtil;

    @GetMapping("/testRedisShareLock")
    public String testRedisShareLock() {
        boolean result = redisShareLockUtil.lock("qj", "123456", 100000L);
        log.info("测试分布式锁获取：{}", result);
        return String.valueOf(result);
    }

    @GetMapping("/testLog")
    public void testLog() {
        long startTime = System.currentTimeMillis();
        for(int i=1;i<100000;i++){
            log.info("这是日志：{}", i);
        }
        long endTime = System.currentTimeMillis();
        log.info("总共耗时：{}", endTime-startTime);
    }

    @GetMapping("/testLocalCache")
    public void testLocalCache(List<Long> skuIdList) {
        cacheUtil.getResult(skuIdList, "skuInfo.skuName", SkuInfo.class, (list) -> {
            Map<Long, SkuInfo> skuInfo = getSkuInfo(skuIdList);
            return skuInfo;
        });
        cacheUtil.getResult(skuIdList, "skuInfo.skuPrice", SkuPrice.class, (list) -> {
            Map<Long, SkuPrice> skuPrice = getSkuPrice(skuIdList);
            return skuPrice;
        });
    }

    // 模拟RPC接口(可以使用OpenFeign来作为Service层的RPC调用)
    public Map<Long, SkuInfo> getSkuInfo(List<Long> skuIdList) {
        return Collections.emptyMap();
    }

    public Map<Long, SkuPrice> getSkuPrice(List<Long> skuIdList) {
        return Collections.emptyMap();
    }

    class SkuInfo {
        private Long id;
        private String name;
    }

    class SkuPrice {
        private Long id;
        private Double price;
    }

}